package Frontend

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"
)

func Admin_get_course(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	sqlStr := `SELECT c.id AS id,c.name AS name,p.name AS profession_name,c.profession_id as profession_id FROM course c LEFT JOIN profession p ON c.profession_id=p.id;`
	type user_data struct {
		Id            int  `db:"id"json:"id"`
		Name          string `db:"name"json:"name"`
		Profession_name string `db:"profession_name"json:"profession_name"`
		Profession_id int `db:"profession_id"json:"profession_id"`
	}
	var users []user_data
	err := db.Select(&users, sqlStr)
	if err != nil {
		fmt.Printf("query failed, err:%v\n", err)
		return
	}
	fmt.Printf("users:%#v\n", users)
	data, err := json.Marshal(users)
	if err != nil {
		fmt.Println("json marshal failed")
		return
	}
	fmt.Printf("json:%s\n", data)
	w.Write([]byte(data)) //给客户端返回数据。
}

func Admin_set_course(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	name:=r.PostFormValue("name")
	profession_id,_:=strconv.Atoi(r.PostFormValue("profession_id"))

	sqlStr := "insert into course(name, profession_id) values (?,?)"
	ret, err := db.Exec(sqlStr, name, profession_id)
	if err != nil {
		fmt.Printf("insert failed, err:%v\n", err)
		return
	}
	theID, err := ret.LastInsertId() // 新插入数据的id
	if err != nil {
		fmt.Printf("get lastinsert ID failed, err:%v\n", err)
		return
	}
	fmt.Printf("insert success, the id is %d.\n", theID)
	jsonk := `{"isok":"ok","msg":"修改成功"}`
	w.Write([]byte(jsonk)) //给客户端返回数据。
}

func Admin_dele_course(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	id,_:=strconv.Atoi(r.PostFormValue("id"))
	type dele_class struct {
		Id            int  `db:"id"json:"id"`
	}
	sqlStr := "select id from class where course_id = ?;"
	var users []dele_class
	err := db.Select(&users, sqlStr, id)
	if err != nil {
		fmt.Printf("query failed, err:%v\n", err)
		return
	}
	fmt.Printf("users:%#v\n", users)
	// 删除班级表
	sqlStr = "delete from class where course_id = ?;"
	ret, err := db.Exec(sqlStr, id)
	if err != nil {
		fmt.Printf("delete failed, err:%v\n", err)
		return
	}
	n, err := ret.RowsAffected() // 操作影响的行数
	if err != nil {
		fmt.Printf("get RowsAffected failed, err:%v\n", err)
		return
	}
	fmt.Printf("delete success, affected rows:%d\n", n)

	// 删除报名记录表
	for i:=0;i<len(users);i++ {
		sqlStr := "delete from register_record where class_id = ?"
		ret, err := db.Exec(sqlStr, users[i].Id)
		if err != nil {
			fmt.Printf("delete failed, err:%v\n", err)
			return
		}
		n, err := ret.RowsAffected() // 操作影响的行数
		if err != nil {
			fmt.Printf("get RowsAffected failed, err:%v\n", err)
			return
		}
		fmt.Printf("delete success, affected rows:%d\n", n)
	}

	// 删除课程表
	sqlStr = "delete from course where id = ?"
	ret, err = db.Exec(sqlStr, id)
	if err != nil {
		fmt.Printf("delete failed, err:%v\n", err)
		return
	}
	n, err = ret.RowsAffected() // 操作影响的行数
	if err != nil {
		fmt.Printf("get RowsAffected failed, err:%v\n", err)
		return
	}
	fmt.Printf("delete success, affected rows:%d\n", n)
	jsonk := `{"isok":"ok","msg":"修改成功"}`
	w.Write([]byte(jsonk)) //给客户端返回数据。
}

func Admin_up_course(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	name:=r.PostFormValue("name")
	profession_id,_:=strconv.Atoi(r.PostFormValue("profession_id"))
	id,_:=strconv.Atoi(r.PostFormValue("id"))
	sqlStr := "update course set name=?,profession_id=? where id = ?;"
	ret, err := db.Exec(sqlStr, name,profession_id,id)
	if err != nil {
		fmt.Printf("update failed, err:%v\n", err)
		return
	}
	n, err := ret.RowsAffected() // 操作影响的行数
	if err != nil {
		fmt.Printf("get RowsAffected failed, err:%v\n", err)
		return
	}
	fmt.Printf("update success, affected rows:%d\n", n)
	jsonk := `{"isok":"ok","msg":"修改成功"}`
	w.Write([]byte(jsonk)) //给客户端返回数据。
}


func Admin_get_profession(w http.ResponseWriter, r *http.Request)  {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	sqlStr := "select * from profession;"
	var users []mysql_profession
	err := db.Select(&users, sqlStr)
	if err != nil {
		fmt.Printf("query failed, err:%v\n", err)
		return
	}
	//fmt.Printf("users:%#v\n", users)
	data, err := json.Marshal(users)
	if err != nil {
		fmt.Println("json marshal failed")
		return
	}
	//fmt.Printf("json:%s\n", data)
	w.Write([]byte(data)) //给客户端返回数据。
}